package com.hfy.pcberpbackend.controller;

import com.hfy.pcberpbackend.entity.Employee;
import com.hfy.pcberpbackend.service.EmployeeService;
import com.hfy.pcberpbackend.dto.EmployeeQueryDTO;
import com.hfy.pcberpbackend.dto.BatchDeleteDTO;
import com.hfy.pcberpbackend.common.Result;
import com.hfy.pcberpbackend.common.PageResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 员工Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/employees")
@RequiredArgsConstructor
@Validated
public class EmployeeController {
    
    private final EmployeeService employeeService;
    
    /**
     * 获取员工列表
     */
    @GetMapping
    public Result<PageResult<Employee>> getEmployeeList(EmployeeQueryDTO query) {
        log.info("获取员工列表，查询条件：{}", query);
        
        try {
            PageResult<Employee> result = employeeService.getEmployeePage(query);
            return Result.success("获取成功", result);
        } catch (Exception e) {
            log.error("获取员工列表失败", e);
            return Result.error("获取员工列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据ID获取员工详情
     */
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable Long id) {
        log.info("根据ID获取员工详情，ID：{}", id);
        
        try {
            Employee employee = employeeService.getEmployeeById(id);
            return Result.success("获取成功", employee);
        } catch (RuntimeException e) {
            log.error("获取员工详情失败", e);
            return Result.notFound(e.getMessage());
        } catch (Exception e) {
            log.error("获取员工详情失败", e);
            return Result.error("获取员工详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 新增员工
     */
    @PostMapping
    public Result<Map<String, Long>> createEmployee(@Valid @RequestBody Employee employee) {
        log.info("新增员工：{}", employee);
        
        try {
            Long id = employeeService.createEmployee(employee);
            Map<String, Long> result = new HashMap<>();
            result.put("id", id);
            return Result.success("新增成功", result);
        } catch (RuntimeException e) {
            log.error("新增员工失败", e);
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            log.error("新增员工失败", e);
            return Result.error("新增员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新员工信息
     */
    @PutMapping("/{id}")
    public Result<Void> updateEmployee(@PathVariable Long id, @Valid @RequestBody Employee employee) {
        log.info("更新员工信息，ID：{}，数据：{}", id, employee);
        
        try {
            employeeService.updateEmployee(id, employee);
            return Result.success("更新成功");
        } catch (RuntimeException e) {
            log.error("更新员工失败", e);
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            log.error("更新员工失败", e);
            return Result.error("更新员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除员工
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteEmployee(@PathVariable Long id) {
        log.info("删除员工，ID：{}", id);
        
        try {
            employeeService.deleteEmployee(id);
            return Result.success("删除成功");
        } catch (RuntimeException e) {
            log.error("删除员工失败", e);
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            log.error("删除员工失败", e);
            return Result.error("删除员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除员工
     */
    @DeleteMapping("/batch")
    public Result<Void> batchDeleteEmployees(@Valid @RequestBody BatchDeleteDTO batchDeleteDTO) {
        log.info("批量删除员工，IDs：{}", batchDeleteDTO.getIds());
        
        try {
            employeeService.batchDeleteEmployees(batchDeleteDTO.getIds());
            return Result.success("批量删除成功");
        } catch (RuntimeException e) {
            log.error("批量删除员工失败", e);
            return Result.badRequest(e.getMessage());
        } catch (Exception e) {
            log.error("批量删除员工失败", e);
            return Result.error("批量删除员工失败：" + e.getMessage());
        }
    }
    
    /**
     * 导出员工Excel
     */
    @GetMapping("/export")
    public Result<String> exportEmployees(EmployeeQueryDTO query) {
        log.info("导出员工Excel，查询条件：{}", query);
        
        try {
            // TODO: 实现Excel导出功能
            return Result.success("导出功能开发中");
        } catch (Exception e) {
            log.error("导出员工Excel失败", e);
            return Result.error("导出员工Excel失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取员工角色选项
     */
    @GetMapping("/roles")
    public Result<List<Map<String, String>>> getEmployeeRoles() {
        log.info("获取员工角色选项");
        
        try {
            List<Map<String, String>> roles = employeeService.getEmployeeRoles();
            return Result.success("获取成功", roles);
        } catch (Exception e) {
            log.error("获取员工角色选项失败", e);
            return Result.error("获取员工角色选项失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取业务经理选项
     */
    @GetMapping("/managers")
    public Result<List<Map<String, Object>>> getManagerOptions() {
        log.info("获取业务经理选项");
        
        try {
            List<Map<String, Object>> managers = employeeService.getManagerOptions();
            return Result.success("获取成功", managers);
        } catch (Exception e) {
            log.error("获取业务经理选项失败", e);
            return Result.error("获取业务经理选项失败：" + e.getMessage());
        }
    }
    
    /**
     * 模糊搜索员工
     * 支持按员工代码、员工姓名进行模糊查询
     */
    @GetMapping("/search")
    public Result<List<Employee>> searchEmployees(
            @RequestParam String keyword,
            @RequestParam(required = false) String role,
            @RequestParam(defaultValue = "20") Integer size) {
        log.info("模糊搜索员工，关键词：{}，角色：{}，数量限制：{}", keyword, role, size);
        
        try {
            List<Employee> employees = employeeService.searchEmployees(keyword, role, size);
            return Result.success("搜索成功", employees);
        } catch (Exception e) {
            log.error("模糊搜索员工失败", e);
            return Result.error("模糊搜索员工失败：" + e.getMessage());
        }
    }
} 